package me.firstapp.service.system.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.MD5Utils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.system.Operator;
import me.firstapp.repository.system.OperatorRepository;
import me.firstapp.service.system.OperatorService;

@Service
public class OperatorServiceImpl implements OperatorService {

	@Autowired
	private OperatorRepository operatorRepository;

	@Override
	public Operator findOperatorById(Long id, Long apiClientId) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "id");
		}
		return operatorRepository.findOperatorById(id, apiClientId);
	}

	@Override
	public Operator findOperatorByEmail(String email, Long apiClientId) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		if (StrUtils.isNULL(email)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "邮箱");
		}
		return operatorRepository.findOperatorByEmail(email, apiClientId);
	}

	@Override
	public Operator addOperator(String email, String password, String name, Long apiClientId) {
		if (StrUtils.isNULL(email)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "邮箱");
		}
		if (StrUtils.isNULL(password)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "密码");
		}
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		if (StrUtils.isNULL(name)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户名");
		}
		Operator operator = operatorRepository.findOperatorByName(name, apiClientId);
		if (operator != null) {
			throw new ServiceException(StatusHouse.OPERATOR_NAME_EXIST);
		}
		operator = operatorRepository.findOperatorByEmail(email, apiClientId);
		if (operator != null) {
			throw new ServiceException(StatusHouse.OPERATOR_EMAIL_EXIST);
		}
		operator = new Operator();
		operator.setApiClientId(apiClientId);
		operator.setEmail(email);
		operator.setName(name);
		operator.setPassword(MD5Utils.generate(password));
		operator.setStatus(0);
		operatorRepository.save(operator);
		return operator;
	}

	@Override
	public Operator executeLogin(String name, String password, String ip, Long apiClientId) {
		if (StrUtils.isNULL(name)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "账号");
		}
		if (StrUtils.isNULL(password)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "密码");
		}
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		Operator operator = operatorRepository.findOperatorByName(name, apiClientId);
		if (operator == null) {
			throw new ServiceException(StatusHouse.OPERATOR_NOT_EXIST);
		}
		if (!MD5Utils.verify(password, operator.getPassword())) {
			throw new ServiceException(StatusHouse.OPERATOR_PASSWORD_WRONG);
		}
		operator.setLoginCounts(operator.getLoginCounts() + 1);
		operator.setLastLoginIP(operator.getNowLoginIP());
		operator.setLastLoginTime(operator.getNowLoginTime());
		operator.setNowLoginIP(ip);
		operator.setNowLoginTime(new Date());
		operatorRepository.update(operator);
		return operator;
	}

	@Override
	public Page<Operator> findOperatorPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Long apiClientId) {
		return operatorRepository.findOperatorPage(pageNo, pageSize, searchParams, sortParam, sortType, apiClientId);
	}

	@Override
	public Operator executeAlterPassword(Long id, String oldPassword, String newPassword, Long apiClientId) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "id");
		}
		if (StrUtils.isNULL(oldPassword)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "密码");
		}
		if (StrUtils.isNULL(newPassword)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "密码");
		}
		Operator operator = operatorRepository.findOperatorById(id, apiClientId);
		if (operator == null) {
			throw new ServiceException(StatusHouse.OPERATOR_NOT_EXIST);
		}
		if (!MD5Utils.verify(oldPassword, operator.getPassword())) {
			throw new ServiceException(StatusHouse.OPERATOR_PASSWORD_WRONG);
		}
		operator.setPassword(MD5Utils.generate(newPassword));
		operatorRepository.update(operator);
		return operator;
	}

	@Override
	public Operator updateStatus(Long id, Integer status, Long apiClientId) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "id");
		}
		Operator operator = operatorRepository.findOperatorById(id, apiClientId);
		if (operator == null) {
			throw new ServiceException(StatusHouse.OPERATOR_NOT_EXIST);
		}
		if (status != null) {
			operator.setStatus(status);
		}
		operatorRepository.update(operator);
		return operator;
	}

	@Override
	public Operator findOperatorByName(String name, Long apiClientId) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		if (StrUtils.isNULL(name)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "name");
		}
		return operatorRepository.findOperatorByName(name, apiClientId);
	}

}
